package com.fingard.dsp.bank.directbank.smbc01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;

import java.io.File;
import java.util.Calendar;
import java.util.Date;

/**
 * 三井住友
 * @version 1.0
 */
public class SMBC01Balance extends SMBC01Base implements IBalance {

    /**
     * 今日余额查询 MT942
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        return null;
    }

    /**
     * 历史查询 MT940
     */
    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try{
            String accNum = p_req7505.actNum;
            Date dateStart = Format.Date8Format.parse(p_req7505.dateFrom);
            Date dateEnd = Format.Date8Format.parse(p_req7505.dateTo);
            Calendar calendar=Calendar.getInstance();
            while (!dateStart.after(dateEnd)){
                String dealedPath=getBankConfig().getValueAsString("DealedPath")+File.separator+
                        Format.Date8Format.format(dateStart)+File.separator+"MT940"+File.separator+
                        accNum+convertCurToBank(p_req7505.actCur)+"_7505.txt";
                if(!FileHelper.existFile(dealedPath)){
                    dealedPath = getBankConfig().getValueAsString("DealedPath")+File.separator+
                            Format.Date8Format.format(dateStart)+File.separator+"MT940"+File.separator+
                            accNum+"_7505.txt";
                    if(!FileHelper.existFile(dealedPath)){
                        calendar.setTime(dateStart);
                        calendar.add(Calendar.DAY_OF_MONTH, 1);
                        dateStart = calendar.getTime();
                        continue;
                    }
                }
                //读取文件内容
                String balanceContent = FileHelper.readFileText(dealedPath,getCharset());
                WriteBankLogLn("读取到的文件内容："+balanceContent);
                if(StringHelper.hasAnyChar(balanceContent)){
                    //账号|币种|当前余额|可用余额|昨日余额(MT940)|页码
                    String[] bal  =balanceContent.split("\\|");
                    RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                    tmpRetDetail.actBal=bal[2].trim();
                    tmpRetDetail.availBal=bal[3].trim();
                    tmpRetDetail.balDate=Format.Date10Format.format(dateStart);
                    retAts.writeRetDetail(tmpRetDetail);
                }else {
                    tmpRespCode="-1";
                    tmpRespInfo="文件读取异常";
                }
                calendar.setTime(dateStart);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                dateStart = calendar.getTime();
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
        }
        retAts.writeEnd();
        return retAts;
    }
}
